package com.invengo.module_robot.ui.fragment.bookinfo;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.GridLayoutManager;

import com.invengo.library_orionstar.RobotConstant;
import com.invengo.library_orionstar.RobotManager;
import com.invengo.module_robot.BR;
import com.invengo.module_robot.R;
import com.invengo.module_robot.databinding.FragmentBookInfoBinding;

import me.goldze.mvvmhabit.base.BaseFragment;

public class BookInfoFragment extends BaseFragment<FragmentBookInfoBinding, BookInfoViewModel> {
   private  OpenCloseAdapter adapter = new OpenCloseAdapter();

    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.fragment_book_info;
    }
    @Override
    public void onDestroyView() {
        binding.speechView.unRegister();
        super.onDestroyView();
    }


    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public void initData() {
        RobotManager.getInstance().changeTip(RobotConstant.TIP_INFO);
        binding.recyclerView.setLayoutManager(new GridLayoutManager(getActivity(),3){
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        });
        binding.recyclerView.setAdapter(adapter);
        binding.recyclerView.setFocusable(false);
        getData();
//
    }
    private void getData(){
        String isbn = getArguments().getString("isbn");
        String image = getArguments().getString("image");
        String content = getArguments().getString("content");
        String bookName = getArguments().getString("bookName");
        String author = getArguments().getString("author");
        String publisher = getArguments().getString("publisher");
        viewModel.initData(isbn,image,content,bookName,author,publisher);
    }

    @Override
    public void initViewObservable() {
        viewModel.ui.openCloseEvent.observe(this, aBoolean -> {
            adapter.setShowOnlyThree(aBoolean);
        });
        viewModel.ui.locationEvent.observe(this,list -> {
            binding.openClose.setVisibility(list.size()>3? View.VISIBLE:View.INVISIBLE);
            adapter.setNewData(list);
        });
    }
}
